package com.gxuwz.adminprovider.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.gxuwz.adminprovider.mapper.AdminMapper;
import com.gxuwz.adminprovider.mapper.UserMapper;
import com.gxuwz.commonmodule.entity.Account;
import com.gxuwz.commonmodule.entity.Admin;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class AccountController {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private AdminMapper adminMapper;


    /**
     * 登陆
     * @param username
     * @param password
     * @param role
     * @return
     */
    @GetMapping("/login/{username}/{password}/{role}")
    public Account login(@PathVariable("username") String username,@PathVariable("password") String password,@PathVariable("role") String role)
    {
        Account account=null;
        switch (role)
        {
            case "读者":
                account=userMapper.login(username,password,role);
                break;
            case "管理员":
                account=adminMapper.login(username,password,role);
                break;
        }
        return  account;
    }

    /**
     * 注册
     * @param username
     * @param password
     * @param role
     * @return
     */
    @RequestMapping(value = "/register",method = RequestMethod.GET)
    public int register(@RequestParam(value = "username") String username,
                        @RequestParam(value = "name")String name,
                        @RequestParam(value = "password") String password,
                        @RequestParam(value = "email")String email,
                        @RequestParam(value = "phone")String phone,
                        @RequestParam(value = "address")String address,
                        @RequestParam(value = "balance")String balance,
                        @RequestParam(value = "role") String role) {
        return userMapper.register(username, name, password,email,phone,address,balance,role);
    }

    /**
     * 通过id查询管理员信息
     * @param username
     * @return
     */
    @GetMapping("/findById/{username}")
    public Admin getAdmin(@PathVariable("username") String username){
        return adminMapper.findByID(username);
    }

    /**
     * 查询所有管理员信息
     * @param admin
     * @return
     */
    @GetMapping("/list")
    public List<Admin> list(@RequestBody Admin admin){
        return adminMapper.query(admin);
    }

    /**
     * 管理员个人信息更新
     * @param username
     */
    @GetMapping("/{username}")
    public Admin findInfo(@PathVariable("username") String username){
        return adminMapper.findInfo(username);
    }

    /**
     * 添加管理员信息
     * @param admin
     */
    @PostMapping("/add")
    public void addAdmin(@RequestBody Admin admin){
        adminMapper.insert(admin);
    }

    /**
     * 通过username删除管理员信息
     * @param username
     */
    @DeleteMapping("/delete/{username}")
    public void deleteAdmin(@PathVariable("username") String username){
        adminMapper.delete(username);
    }

    /**
     * 更新管理员信息
     * @param admin
     */
    @PutMapping("/update")
    public void editAdmin(@RequestBody Admin admin){
        adminMapper.update(admin);
    }

    /**
     * 分页（不带条件）
     * @param pageIndex
     * @param pageSize
     * @return
     */
    @GetMapping(value = "/findPage")
    public PageInfo findPage(@RequestParam(value = "pageIndex") int pageIndex, @RequestParam(value = "pageSize") int pageSize){
        PageHelper.startPage(pageIndex,pageSize);
        List<Admin> admins = adminMapper.findPage(null);
        PageInfo<Admin> page = new PageInfo<>(admins);
        return page;
    }


}
